package com.jlu155.CourseManager.controller;

import com.jlu155.CourseManager.Bean.LoginRequest;
import com.jlu155.CourseManager.Bean.PageRequest;
import com.jlu155.CourseManager.Bean.PageResult;
import com.jlu155.CourseManager.entity.Course;
import com.jlu155.CourseManager.entity.Message;
import com.jlu155.CourseManager.entity.Student;
import com.jlu155.CourseManager.entity.Teacher;
import com.jlu155.CourseManager.repository.StudentCourseRepository;
import com.jlu155.CourseManager.repository.StudentRepository;
import com.jlu155.CourseManager.service.StudentCourseService;
import com.jlu155.CourseManager.service.StudentService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class StudentController {
    //Autowired告诉spring自动获取studentRepository
    @Autowired
    private StudentRepository studentRepository;
    @Autowired
    private StudentService studentService;
    //这样就能处理对应/api/addHello的请求
    @PostMapping("/addHello")
    public Message addHello(@RequestBody Message request) {
        String result = "Hello, "+request.getName();
        return new Message(result);
    }

    @PostMapping("/getStudents")
    public PageResult<Student> getStudents(@RequestBody PageRequest pageRequest){

        return studentService.getStudentPage(pageRequest);
    }

    @PostMapping("/insertStudent")
    public ResponseEntity insertStudent(@RequestBody Student student, HttpSession session){
        Integer teacher_type=(Integer)session.getAttribute("teacher_type");
        if(teacher_type!=1)
            return ResponseEntity.badRequest().body("权限不足");
        if(studentRepository.findStudentById(student.getSid())!=null)
            return ResponseEntity.badRequest().body("错误，学生已存在");
        studentRepository.insertStudent(student);
        return ResponseEntity.ok().body("增加学生成功");
    }
    @PostMapping("/updateStudent")
    public ResponseEntity updateStudent(@RequestBody Student student,HttpSession session){
        Integer teacher_type=(Integer)session.getAttribute("teacher_type");
        if(teacher_type!=1)
            return ResponseEntity.badRequest().body("权限不足");
        if(studentRepository.findStudentById(student.getSid())==null)
            return ResponseEntity.badRequest().body("错误，学生不存在");
        studentRepository.updateStudent(student);
        return ResponseEntity.ok().body("修改学生成功");
    }

    @PostMapping("/deleteStudent")
    public ResponseEntity deleteStudents(@RequestBody Integer sid){
        try{
            studentRepository.deleteStudent(sid);
            return ResponseEntity.ok(true);
        }
        catch(Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        }
    }
    @RequestMapping("/studentLogin")
    public ResponseEntity login(@RequestBody LoginRequest loginInfo, HttpSession session){
        boolean valid=studentService.loginValidate(loginInfo.getId(),loginInfo.getPasswd());
        if(valid) {
            Student student=studentRepository.findStudentById(loginInfo.getId());
            session.setAttribute("student_id", loginInfo.getId());
            return ResponseEntity.ok().body("student");
        }
        else {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(false);
        }
    }
    @RequestMapping("/updateStudentPasswd")
    public ResponseEntity updateStudentPasswd (@RequestBody String passwd,HttpSession session){
        Integer sid= (Integer) session.getAttribute("student_id");
        if(sid==null){
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("登录无效");
        }
        try{
            studentRepository.updatePasswdById(sid,passwd);
            return ResponseEntity.ok().body(null);
        }catch (Exception e){
            return ResponseEntity.internalServerError().body(e.getMessage());
        }
    }
}
